import java.util.Enumeration;
import java.util.Vector;

/**
 * ResultSet.java
 * 
 * The class represents the result set of a query.
 * 
 * @author Zachary M. Allen
 */
public class ResultSet {

	/**
	 * The list of instances in this result set
	 */
	private Vector instances;

	/**
	 * Constructor
	 *
	 */
	public ResultSet() {
		instances = new Vector();
	}

	/**
	 * Adds an instance of a word on a line in a file to this result set
	 * @param    word    Word of instance
	 * @param    file    File that instance is in
	 * @param    line    Line that instance is on
	 * @param    variableBreakCharacters    Variable break characters used in creating corpus
	 * @throws    QUIRKException
	 */
	public void add(String word, String file, int line, String variableBreakCharacters) throws QUIRKException {
		instances.add(new Instance(word, file, line, variableBreakCharacters));
	}

	/**
	 * Getter returns value of instances
	 * @return    Current value of instances
	 */
	public Vector getInstances() {
		return instances;
	}

	/**
	 * Returns a String representation of this result set by concatenating
	 * the string representations of all of the instances in this result set 
	 * @return    String representation of this result set
	 */
	public String toString() {
		String retval = "";
		for (Enumeration e = instances.elements(); e.hasMoreElements();) {
			Instance i = (Instance) e.nextElement();
			retval += i.toString();
		}
		return retval;
	}

	/**
	 * Sets this result set to the intersection of the current result set and 
	 * the parameter
	 * @param    temp    Result set to take the intersection with
	 */
	public void intersect(ResultSet temp) {
		Vector newInstances = new Vector();
		for (Enumeration a = temp.getInstances().elements(); a.hasMoreElements();) {
			Instance i = (Instance) a.nextElement();
			for (Enumeration b = instances.elements(); b.hasMoreElements();) {
				Instance j = (Instance) b.nextElement();
				if (i.equals(j)) {
					if (i.isFalsePositive() || j.isFalsePositive()) {
						i.setFalsePositive(true);
					}
					newInstances.add(i);
				}
			}
		}
		instances = newInstances;
	}

	/**
	 * Sets this result set to the union of the current result set and 
	 * the parameter
	 * @param    temp    Result set to take the union with
	 */
	public void union(ResultSet temp) {
		instances.addAll(temp.getInstances());
		for (Enumeration a = instances.elements(); a.hasMoreElements();) {
			Instance i = (Instance) a.nextElement();
			for (Enumeration b = instances.elements(); b.hasMoreElements();) {
				Instance j = (Instance) b.nextElement();
				if (i.equals(j) && i != j) {
					if (!i.isFalsePositive() || !j.isFalsePositive()) {
						i.setFalsePositive(false);
					}
					instances.remove(j);
				}
			}
		}
	}
	
	/**
	 * Returns the number of false positives in this result set
	 * @return   Number of false positives in this result set
	 */
	public int getNumberOfFalsePositives() {
		int retval = 0;
		for (Enumeration e = instances.elements(); e.hasMoreElements();) {
			Instance i = (Instance) e.nextElement();
			if (i.isFalsePositive()) {
				retval++;
			}
		}
		return retval;
	}

}
